package sunbao.ido.experimentalField.listener;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * \* User: MeiZhongHao
 * \* Date: 2018-11-13
 * \* Time: 19:25
 * \* Description:
 * \
 */
@Configuration
public class SmsAutoConfiguration {

    @Bean
    public SmsAutoConfiguration.SmsBeanFactoryPostProcessor smsBeanFactoryPostProcessor() {
        return new SmsAutoConfiguration.SmsBeanFactoryPostProcessor();
    }

    @Bean
    public SmsAutoConfiguration.RegisterSmsChangeListenerBeanPostProcessor registerSmsChangeListenerBeanPostProcessor() {
        return new SmsAutoConfiguration.RegisterSmsChangeListenerBeanPostProcessor();
    }

    static class RegisterSmsChangeListenerBeanPostProcessor implements BeanPostProcessor {
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            return bean;
        }

        /***
         * 注册代码定义的SmsEventListener
         * @param bean
         * @param beanName
         * @return
         * @throws BeansException
         */
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof SmsEventListener) {
                SmsManager.addSmsEventListener((SmsEventListener) bean);
            }
            return bean;
        }
    }

    static class SmsBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
        /***
         * 注册需要执行批量操作的SmsEventListener
         * @param beanFactory
         * @throws BeansException
         */
        @Override
        public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
            String[] var2 = beanFactory.getBeanDefinitionNames();
            SmsManager.addSmsEventListener(event -> {
                String[] var21 = beanFactory.getBeanDefinitionNames();
                int var3 = var21.length;

                for (int var4 = 0; var4 < var3; ++var4) {
                    String beanName = var21[var4];
                }
                // 如： 执行@Value赋值操作
            });
        }
    }
}